#coding=utf-8

import unittest
from selenium import webdriver
from appium import webdriver
from time import sleep
from nav_plugin_op import nav_plugin_factory
from selenium.common.exceptions import WebDriverException
import time
from Public import BasePage
from Tool.usefulTools import userfulToolsFactory
from Public.BasePage import element_Oprate
from Public.BasePage import BasicAction
from Public import BaseConstant
import settings

#实例化基础数据
navOpenFactory = nav_plugin_factory()
userfulTool = userfulToolsFactory()
element_oprate_instance = element_Oprate()
element_find_instance = BasicAction()
General_queryp_Paramter_instances = BaseConstant.General_queryp_Paramter
nav_name = ["导航栏工作",'xpath',"//android.widget.LinearLayout/android.widget.LinearLayout[2]/android.widget.FrameLayout/android.widget.RadioButton"]
plugin_name = ["插件列表-通用查询",'name','通用查询']	

timestr = time.strftime('%Y%m', time.localtime(time.time()))
query_TaskNumber_keys =  str(timestr) # 查询任务号
Description_keys = "添加描述"
query_txtRange_keys = "1000"

class general_query_factory():

	def open_generalquery_plug(self,driver,file_path):
		try:
			element_oprate_instance.element_click(nav_name,file_path)
			# 点击"通用查询"的插件，打开插件
			element_oprate_instance.element_click(plugin_name,file_path)
			#等待通用查询页面内容进行加载，加载完成之后进行页面确认并界面
			sleep(2)
			element_oprate_instance.element_save_pic(file_path, 'open_generalquery_plug_successful')

		except WebDriverException,error:
			print error
			element_oprate_instance.element_save_pic(file_path, 'open_generalquery_plug_fail')
			raise

	def query_case(self,driver,file_path):

		try:
			#设置查询开始日期
			element_oprate_instance.element_click(General_queryp_Paramter_instances.query_StartDate_id, file_path)
			element_oprate_instance.element_click(General_queryp_Paramter_instances.set_up_id, file_path)
			#查询结束日期
			element_oprate_instance.element_click(General_queryp_Paramter_instances.query_EndDate_id, file_path)
			element_oprate_instance.element_click(General_queryp_Paramter_instances.set_up_id, file_path)
			#输入任务号
			element_oprate_instance.element_click(General_queryp_Paramter_instances.query_TaskNumber_id, file_path)
			element_oprate_instance.element_send_keys(General_queryp_Paramter_instances.query_TaskNumber_id, query_TaskNumber_keys.decode("utf-8"), file_path)
			#设置类型
			sleep(2)
			element_oprate_instance.element_click(General_queryp_Paramter_instances.query_EventType_id, file_path)
			element_oprate_instance.element_click(General_queryp_Paramter_instances.query_Type_keys, file_path)
			#设置大类
			sleep(2)
			element_oprate_instance.element_click(General_queryp_Paramter_instances.query_MainType_id, file_path)
			element_oprate_instance.element_click(General_queryp_Paramter_instances.query_Type_keys, file_path)
			#设置小类
			element_oprate_instance.element_click(General_queryp_Paramter_instances.query_SubType_id, file_path)
			element_oprate_instance.element_click(General_queryp_Paramter_instances.query_Type_keys, file_path)
			#勾选本部门
			#driver.find_element_by_id(query_cbunit_id).click()
			#勾选案件状态（以结案为例）
			element_oprate_instance.element_click(General_queryp_Paramter_instances.query_StatusClosed_id, file_path)
			#地图定位
			# driver.find_element_by_id(query_Map_id).click()
			# driver.find_element_by_id(map_bottom_id).click()
			# #添加描述，先滑动屏幕################################################
			# driver.swipe(300, 600, 300, 300)
			# driver.find_element_by_id(btnDescription_id).click()
			# driver.find_element_by_id(btnDescription_id).send_keys(Description_keys.decode("utf-8"))
			# #输入查询范围
			# driver.find_element_by_id(query_txtRange_id).click()
			# driver.find_element_by_id(query_txtRange_id).send_keys(query_txtRange_keys.decode("utf-8"))
			# #勾选上报人
			# driver.find_element_by_id(ReporterSelf_id).click()			
			#点击查询
			element_oprate_instance.element_click(General_queryp_Paramter_instances.btnQuery_id, file_path)
			element_oprate_instance.element_save_pic(file_path, 'query_case_result')
			# element_oprate_instance.element_click(General_queryp_Paramter_instances.exit_id, file_path)
			# element_oprate_instance.element_click(General_queryp_Paramter_instances.exit_id, file_path)
			sleep(5)

		except WebDriverException,error:
			print error
			element_oprate_instance.element_save_pic(file_path, 'query_case_fail')
			raise

	#检查通用查询的结果
	def query_result_check(self):
		print u'进行通用查询结果检查，检查是否查询到数据'
		#获取查询结果的数量信息
		query_result_text = element_find_instance.find_element(General_queryp_Paramter_instances.back_to_up,settings.pic_file_path).get_attribute('text')
		if '(' in str(query_result_text):
			print u'通用查询成功'
			element_oprate_instance.element_save_pic(settings.pic_file_path,'query_case_result_pass')
		else:
			print u'通用查询失败，未查询到任何结果'
			element_oprate_instance.element_save_pic(settings.pic_file_path, 'query_case_result_failed')
			raise str(query_result_text)

if __name__ == '__main__':
	unittest.main()






 